<?php
/**
 * Created by PhpStorm.
 * Athor  By yuan xi
 * Email  By shen3@vip.qq.com
 * ip控制器增删改
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table=Table('admin_ip');

if(IS_POST){
    if($a == 'add' || $a == 'edit' ){
        $id = intval($_REQUEST['id']);
        //  表单数据
        $post = $_POST;
        $ins = [];
        $ins['ip']        =   NoBadStr($post['ip']);
        $ins['describe']  =   NoBadStr($post['describe']);

        if($id){
            //修改
            if(empty($ins['ip'])) ShowMsg('ip必须填写',U($m."/".$c."/".$a,['id'=>$id]));
            $flag = $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$id);
        }else{
            if(empty($ins['ip'])) ShowMsg('ip必须填写',U($m."/".$c."/".$a));
            $flag = $db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
        }
        if ($flag){
            ShowMsg('操作成功',U($m."/".$c));
            // AjaxReturn(['error'=>'0','msg'=>'操作成功']);
        }else{
            ShowMsg('操作失败',U($m."/".$a));
            // AjaxReturn(['error'=>'1','msg'=>'操作失败']);
        }
    }
    //验证
    if($a=='verify'){
        $ip=trim(NoBadStr($_POST['ip']));
        $sql = " select ip from ".$table." where ip = '$ip'";
        $res = $db->getrow($sql);
        if($res){
            AjaxReturn(['error'=>'1','msg'=>'ip已经存在']);
        }
        die;
    }
    //删除
    else if($a == 'del'){
        $ids = $_POST['ids'];
        foreach ($ids as $k => $v) {
            $ids[$k] = trim(NoBadStr($v));
        }
        $sql = "select id from ".$table." where id in (".implode(',', $ids).")";
        $rows = $db->affected_rows($sql);
        ($rows != count($ids)) && AjaxReturn(['error'=>'1','msg'=>'操作失败']);
        $db->exec("delete from ".$table." WHERE id in (".implode(',', $ids).")");
        AjaxReturn(['error'=>'0','msg'=>"操作".$rows."条数据成功"]);
    }else{
        echo '非法操作';exit();
    }


}


//增加显示
if($a=='add'){

    $tpl->display('admin/ip_add');
    die;
}
if($a=='edit'){
    $id = intval($_REQUEST['id']);
    $sql = "select * from  $table ".' where id = '.$id;
    $data = $db->getrow($sql);
    $tpl->display('admin/ip_edit');
    die;
}


//显示所有
$sql = "select * from  $table ";
$data = $db->getall($sql);

$tpl->display('admin/ip');